package cn.web.management.web.servlet.account;

import cn.web.management.domain.UserInform;
import cn.web.management.service.inface.LoginVerify;
import cn.web.management.service.impl.LoginVerifyImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet(name = "loginVerifyServlet", value = "/loginVerifyServlet")
public class loginVerifyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    /**
     * @description: 登录身份验证Servlet，返回字符串true或者false代表验证身份是否成功
     * @paramName: [request, response]
     * @paramType: [javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse]
     * @returnType: void
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String identity = request.getParameter("identity");

//        try {
//            Thread.sleep(5000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }

        LoginVerify loginVerify = new LoginVerifyImpl();
        List<UserInform> list = loginVerify.findUserNamePassword();

        UserInform u;
        boolean flag = false;
        for (int i = 0; i < list.size(); i++) {
            u = list.get(i);
            if(u.getIdentity().equals(identity))
                if(u.getUsername().equals(username))
                    if(u.getUserpassword().equals(password)){
                        response.getWriter().write("true");
                        flag = true;
                        break;
                    }
        }
        if (!flag){
            response.getWriter().write("false");
        }
    }
}
